{{/* 
  图片处理器
*/}}

{{- $context := .context -}}
{{- $src := .src | string -}}
{{- $alt := .alt | default "" -}}
{{- $caption := .caption | default "" -}}
{{- $enablePlaceholder := .enablePlaceholder | default true -}}
{{- $outputType := .outputType | default "both" -}}

{{/* 初始化核心变量 */}}
{{- $resource := false -}}
{{- $resourceType := "" -}}
{{- $isExternal := false -}}
{{- $finalUrl := "" -}}

{{/* 检查是否为外部URL */}}
{{- if or (hasPrefix $src "http://") (hasPrefix $src "https://") -}}
  {{- $isExternal = true -}}
  {{- $resourceType = "external" -}}
  {{- $finalUrl = $src -}}
{{- else -}}
  {{/* 本地资源处理 */}}
  {{- $searchPath := $src -}}
  {{- if hasPrefix $src "./" -}}
    {{- $searchPath = strings.TrimPrefix "./" $src -}}
  {{- else if hasPrefix $src "/" -}}
    {{- $searchPath = strings.TrimPrefix "/" $src -}}
  {{- end -}}
  
  {{/* 优先级查找：页面资源 -> 全局资源 -> 静态文件 */}}
  {{- with $context.Resources.GetMatch $searchPath -}}
    {{- $resource = . -}}
    {{- $resourceType = "page" -}}
    {{- $finalUrl = .RelPermalink -}}
  {{- else -}}
    {{- with resources.Get $searchPath -}}
      {{- $resource = . -}}
      {{- $resourceType = "global" -}}
      {{- $finalUrl = .RelPermalink -}}
    {{- else -}}
      {{- $resourceType = "static" -}}
      {{- $finalUrl = $src -}}
    {{- end -}}
  {{- end -}}
{{- end -}}

{{/* 设置核心数据到上下文 */}}
{{- if or (eq $outputType "data") (eq $outputType "both") -}}
  {{- $context.Scratch.Set "imageResource" $resource -}}
  {{- $context.Scratch.Set "imageUrl" $finalUrl -}}
  {{- $context.Scratch.Set "imageType" $resourceType -}}
  {{- $context.Scratch.Set "isExternal" $isExternal -}}
{{- end -}}

{{/* 生成图片HTML */}}
{{- if or (eq $outputType "img") (eq $outputType "both") -}}
  <img
    src="{{ $finalUrl }}"
    alt="{{ $alt }}"
    {{ with $caption }}title="{{ . }}"{{ end }}
    {{ if and $resource (not $isExternal) }}
      {{ $mediaType := $resource.MediaType.SubType }}
      {{ if in (slice "jpeg" "jpg" "png" "gif" "webp" "svg") $mediaType }}
        {{ if ne $mediaType "svg" }}
    width="{{ $resource.Width }}"
    height="{{ $resource.Height }}"
        {{ end }}
      {{ end }}
    {{ end }}
    loading="lazy"
    decoding="async"
    data-gallery-src="{{ $finalUrl }}"
    data-gallery-alt="{{ $alt }}"
    data-gallery-title="{{ $caption }}" />
{{- end -}}